package com.ithiema.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ithiema.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/save")
    public String save(){
        System.out.println("UserController is running");
        return "/success.jsp";
    }


    @RequestMapping("/requestParam1")
    public String requestParam1(String name,Integer age){
        System.out.println(name+",,"+age);
        return "/success.jsp";
    }
    @RequestMapping("/requestParam2")
    public String requestParam2(@RequestParam("username") String name, Integer age){
        System.out.println(name+",,"+age);
        return "/success.jsp";
    }

    @RequestMapping("/requestParam3")
    public String requestParam3(User user){
        System.out.println(user);
        return "/success.jsp";
    }

    @RequestMapping("/requestParam4")
    public String requestParam4(@DateTimeFormat(pattern ="yyyy-MM-dd")Date date){
        System.out.println(date);
        return "/success.jsp";
    }

    @RequestMapping(value = "/responseUserJson")
    @ResponseBody
    public User responseUserJson() throws JsonProcessingException {
        User user=new User();   //实际开发是调用service层方法得到的user对象
        user.setName("张益达");
        user.setAge(22);
        List<String> list = Stream.of("snake", "金甲战士", "张大炮").collect(Collectors.toList());
        user.setNick(list);
        //将user对象转换成json字符串，响应给客户端
        return user;
    }
}
